# frozen_string_literal: true

return if helper.stable_branch?

data_qa_selectors = /qa_selector|data-qa-selector/

deprecated_qa_selectors = /(?!.*\bdata-qa-)(?=class=.*qa-.*|class: .*qa-.*)/

def filter_changed_lines(files, pattern)
  lines = []
  files.each do |file|
    qa_selector_changed_lines = helper.changed_lines(file).select { |line| line =~ pattern }
    next unless qa_selector_changed_lines.any?

    lines += ["file `#{file}`:", qa_selector_changed_lines]
  end
  lines
end

changed_code_files = helper.changed_files(/\.(vue|haml|js|rb)$/)

return if changed_code_files.empty?

lines_with_qa_selectors = filter_changed_lines(changed_code_files, data_qa_selectors)

deprecated_qa_class = filter_changed_lines(changed_code_files, deprecated_qa_selectors)

return if (lines_with_qa_selectors + deprecated_qa_class).empty?

markdown(<<~MARKDOWN)
  ## QA Selectors

MARKDOWN

if lines_with_qa_selectors.any?
  markdown(<<~MARKDOWN)
    The following changed lines in this MR contain QA selectors:

    * #{lines_with_qa_selectors.join("\n* ")}

    Please ensure `e2e:package-and-test` job is run and the tests are passing.

    For the list of known failures please refer to [the latest pipeline triage issue](https://gitlab.com/gitlab-org/quality/pipeline-triage/-/issues).

    If your changes are under a feature flag, please check our [Testing with feature flags](https://docs.gitlab.com/ee/development/testing_guide/end_to_end/feature_flags.html#automatic-test-execution-when-a-feature-flag-definition-changes) documentation for instructions.

  MARKDOWN

  warn "This merge request contains lines with QA selectors. Please ensure `e2e:package-and-test` job is run."
end

if deprecated_qa_class.any?
  markdown(<<~MARKDOWN)
    ### Deprecated .qa-selector class

    The following lines in this MR contain deprecated qa class selectors:

    * #{deprecated_qa_class.join("\n* ")}

    Please ensure all deprecated qa class selectors are replaced with data-qa-selectors in accordance with our [Testing Guide](https://docs.gitlab.com/ee/development/testing_guide/end_to_end/page_objects.html#data-qa-selector-vs-qa-selector).

  MARKDOWN

  warn "This merge request contains deprecated .qa-selector CSS class. Please use data-qa-selector attribute instead."
end
